Now we'll make a company. In this first tutorial we're keeping things simple so we'll just create a Company that has a 1 to many relationship with People, who are its employees:
Update PersonTest.java to remove the two fields, emf and em and the initEmfAndEm() and cleanup() methods. PersonTest.java
packageentity;importstaticorg.junit.Assert.assertEquals;importstaticorg.junit.Assert.assertTrue;importjava.util.List;importorg.junit.Test;publicclass PersonTest extends TestBase {privatefinal Address a1 = new Address("A Rd.", "", "Dallas", "TX", "75001");privatefinal Person p1 = new Person("Brett", 'L', "Schuchert", a1);privatefinal Address a2 = new Address("B Rd.", "S2", "OkC", "OK", "73116");privatefinal Person p2 = new Person("FirstName", 'K', "LastName", a2);
@SuppressWarnings("unchecked")
@Test
publicvoid insertAndRetrieve(){
em.getTransaction().begin();
em.persist(p1);
em.persist(p2);
em.getTransaction().commit();finalList<Person> list = em.createQuery("select p from Person p")
.getResultList();
assertEquals(2, list.size());for(Person current : list){finalString firstName = current.getFirstName();finalString streetAddress1 = current.getAddress()
.getStreetAddress1();
assertTrue(firstName.equals("Brett")
|| firstName.equals("FirstName"));
assertTrue(streetAddress1.equals("A Rd.")
|| streetAddress1.equals("B Rd."));}}}
Make sure everything is green before going on (rerun using Ctrl-F11).
Now we need to create a new CompanyTest class. Here's the first version:
packageentity;importstaticorg.junit.Assert.assertEquals;importorg.junit.Test;publicclass CompanyTest extends TestBase {
@Test
publicvoid createCompany(){final Company c1 = new Company();
c1.setName("The Company");
c1.setAddress(new Address("D Rd.", "", "Paris", "TX", "77382"));
em.getTransaction().begin();
em.persist(c1);
em.getTransaction().commit();final Company foundCompany = (Company) em.createQuery("select c from Company c where c.name=?1").setParameter(1,
"The Company").getSingleResult();
assertEquals("D Rd.", foundCompany.getAddress().getStreetAddress1());// Note, we do not need an assert. Why? the method getSingleResult()// will throw an exception if there is not exactly one// object found. We'll research that in the second JPA tutorial.}}
Run this unit test and make sure it is all green before going on (right-click in the source pane, select Run As:JUnit Test).
If you'd like to run all of your tests, right-click on the test folder, select Run As:JUnit Test and eclipse will execute all of your tests classes' test methods.
Hire some people
We need to create some people and add them to the company. The PersonTest class already has some people. Rather than re-creating new people, let's update PersonTest to make those fields available. Update the a1, p1, a2, and p2 fields as follows:
publicstaticList<Person> generatePersonObjects(){finalList<Person> people = newArrayList<Person>();final Address a1 = new Address("A Rd.", "", "Dallas", "TX", "75001");final Person p1 = new Person("Brett", 'L', "Schuchert", a1);final Address a2 = new Address("B Rd.", "S2", "OkC", "OK", "73116");final Person p2 = new Person("FirstName", 'K', "LastName", a2);
people.add(p1);
people.add(p2);return people;}
You will also need to update the beginning of the method insertAndRetrieve from:
finalList<Person> people = generatePersonObjects();
em.getTransaction().begin();for(Person p : people){
em.persist(p);}
em.getTransaction().commit();
Now we'll add a new test into CompanyTest to verify that we can hire people:
@SuppressWarnings("unchecked")
@Test
publicvoid createCompanyAndHirePeople(){final Company c1 = new Company();
c1.setName("The Company");
c1.setAddress(new Address("D Rd.", "", "Paris", "TX", "77382"));List<Person> people = PersonTest.generatePersonObjects();for(Person p : people){
c1.hire(p);}
em.getTransaction().begin();for(Person p : people){
em.persist(p);}
em.persist(c1);
em.getTransaction().commit();finalList<Person> list = em.createQuery("select p from Person p")
.getResultList();
assertEquals(2, list.size());final Company foundCompany = (Company) em.createQuery("select c from Company c where c.name=?1").setParameter(1,
"The Company").getSingleResult();
assertEquals(2, foundCompany.getEmployees().size());}
Update persistence.xml
Again, given our environment, this step is optional.
Company.java
Factor out Common Test Code
We have some common initialization we can move up into a base since we are going to have two tests classes, PersonTest and CompanyTest:TestBase.java
Update PersonTest.java to remove the two fields, emf and em and the initEmfAndEm() and cleanup() methods.
PersonTest.java
Make sure everything is green before going on (rerun using Ctrl-F11).
Now we need to create a new CompanyTest class. Here's the first version:
Run this unit test and make sure it is all green before going on (right-click in the source pane, select Run As:JUnit Test).
If you'd like to run all of your tests, right-click on the test folder, select Run As:JUnit Test and eclipse will execute all of your tests classes' test methods.
Hire some people
We need to create some people and add them to the company. The PersonTest class already has some people. Rather than re-creating new people, let's update PersonTest to make those fields available. Update the a1, p1, a2, and p2 fields as follows:You will also need to update the beginning of the method insertAndRetrieve from:
to:
Now we'll add a new test into CompanyTest to verify that we can hire people:
Update persistence.xml
Again, given our environment, this step is optional.persistence.xml
Make sure everything compiles and runs green.